﻿#include "logui.h"
#include "ui_logui.h"

logui::logui(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::logui)
{
    ui->setupUi(this);

    this->setFixedSize(this->size());//锁定登陆窗口大小

    if(iniDB() == false){//如果打开数据库失败
        QMessageBox::warning(0,"Warning","Initialization failure.");
        ui->btnLog->setEnabled(false);
        ui->btnSign->setEnabled(false);
        return;
    }
}

logui::~logui()
{
    delete ui;
}


void logui::on_btnQuit_clicked()
{
    this->close();
}

bool logui::iniDB()
{
    //设置连接参数
    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setDatabaseName("db_cureSys");
    m_db.setHostName("localhost");
    m_db.setUserName("root");
    m_db.setPassword("root");
    if(m_db.open() == true){
        return true;//打开数据库成功返回true
    }
    else{
        return false;//打开失败返回false
    }
}


void logui::on_btnLog_clicked()
{
    if(ui->editAccount->text().isEmpty() || ui->editPswd->text().isEmpty()){//如果账号或密码为空则不可以登陆
        QMessageBox::warning(0,"Warning","Please enter full information..");
        return;
    }

    QString account = ui->editAccount->text();//账号
    QString pswd = ui->editPswd->text();//密码
    int role;//角色
    QSqlQuery query;
    if(ui->rbtnGuest->isChecked()){//如果作为客户登陆
        role = 2;
        query.exec("select pswd from tb_sick where account='"+account+"';");//查询该账号的密码
        if(query.size() == 0){//如果没有找到对应的账号的密码
            QMessageBox::warning(0,"Warning","Account error.");
            return;
        }

        //如果找到对应的密码
        query.first();
        if(query.isValid() && query.value("pswd") != pswd){//如果记录不可用或者密码不匹配
            QMessageBox::warning(0,"Warning","Password error.");
            return;
        }

        //进入客户操作界面
        sickUI* s = new sickUI(account,&m_db);
        this->close();
        s->show();

    }else if(ui->rbtnDoctor->isChecked()){//如果作为医生登陆
        role = 1;
        query.exec("select pswd from tb_doctor where account='"+account+"';");//查询该账号的密码
        if(query.size() == 0){//如果没有找到对应的账号的密码
            QMessageBox::warning(0,"Warning","Account error.");
            return;
        }

        //如果找到对应的密码
        query.first();
        if(query.isValid() && query.value("pswd") != pswd){//如果记录不可用或者密码不匹配
            QMessageBox::warning(0,"Warning","Password error.");
            return;
        }

        //进入医生操作界面
        doctorUI* d = new doctorUI(account,&m_db);
        this->close();
        d->show();


    }else if(ui->rbtnAdmin->isChecked()){//如果作为管理员登陆
        role = 0;
        query.exec("select pswd from tb_admin where account='"+account+"';");//查询该账号的密码
        if(query.size() == 0){//如果没有找到对应的账号的密码
            QMessageBox::warning(0,"Warning","Account error.");
            return;
        }

        //如果找到对应的密码
        query.first();
        if(query.isValid() && query.value("pswd") != pswd){//如果记录不可用或者密码不匹配
            QMessageBox::warning(0,"Warning","Password error.");
            return;
        }

        //进入管理员操作界面
        adminUI* a = new adminUI(&m_db);
        this->close();
        a->show();
    }
}


void logui::on_btnSign_clicked()
{
    signUpUI* s = new signUpUI;
    s->exec();
    s->deleteLater();
}

